﻿using Newtonsoft.Json;
using DayuCode.Designer.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DayuCode.Designer.Model
{
    public class SrcCodeConfig
    {
        public long ProjectId { get; set; }

        private Dictionary<string, string> projectPaths = new Dictionary<string, string>();
        public Dictionary<string, string> ProjectPaths
        {
            get { return projectPaths; }
            set { 
                projectPaths = value; 
                if (projectPaths == null) 
                    projectPaths = new Dictionary<string, string>(); 
            }
        }

        public string GetPath(string terminalType, string codeType)
        {
            string key = terminalType + "-" + codeType;
            string path = null;
            if (projectPaths.TryGetValue(key, out path)) return path;
            else return null;
        }

        public void SetPath(string terminalType, string codeType, string path)
        {
            string key = terminalType + "-" + codeType;
            projectPaths[key] = path;
        }
        //public string JavaBasePath { get; set; }

        //public string VueBasePath { get; set; }

        //public string UniappBasePath { get; set; }

        //public string PadBasePath { get; set; }


        private static string getCodeFileConfigFilePath()
        {
            return Path.Combine(AppInstance.StartupPath, "code_path_config.json");

        }

        public static IList<SrcCodeConfig> LoadConfig()
        {
            string codeFileConfigFilePath = getCodeFileConfigFilePath();
            List<SrcCodeConfig> config = null;
            if (File.Exists(codeFileConfigFilePath))
            {
                string jsonStr = File.ReadAllText(codeFileConfigFilePath);
                if (!string.IsNullOrEmpty(jsonStr))
                {
                    try
                    {
                        config = JsonConvert.DeserializeObject<List<SrcCodeConfig>>(jsonStr);
                    }
                    catch
                    {

                    }
                }
            }
            if (config == null)
            {
                config = new List<SrcCodeConfig>(0);
            }
            return config;
        }

        public static void SaveConfig(IList<SrcCodeConfig> config)
        {
            string codeFileConfigFilePath = getCodeFileConfigFilePath();
            string jsonStr = JsonConvert.SerializeObject(config, Formatting.Indented);
            File.WriteAllText(codeFileConfigFilePath, jsonStr);
        }

        /// <summary>
        /// 获取本地代码工程的基础路径
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="codeType"></param>
        /// <returns></returns>
        public static string GetBasePath(long projectId, string terminalType, string codeType)
        {
            string basePath = "";
            IList<SrcCodeConfig> srcCodeConfigs = AppInstance.SourceCodeConfig;
            if (srcCodeConfigs == null)
            {
                srcCodeConfigs = LoadConfig();
                AppInstance.SourceCodeConfig = srcCodeConfigs;
            }
            

            if (srcCodeConfigs != null && srcCodeConfigs.Count > 0)
            {
                foreach (SrcCodeConfig config in srcCodeConfigs)
                {
                    if (config.ProjectId == projectId)
                    {
                        basePath = config.GetPath(terminalType, codeType);
                    }
                }
            }
            return basePath;
        }

        /// <summary>
        /// 设置本地代码工程的基础路径
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="codeType"></param>
        /// <returns></returns>
        public static void SetBasePath(long projectId, string terminalType, string codeType, string basePath)
        {
            IList<SrcCodeConfig> srcCodeConfigs = AppInstance.SourceCodeConfig;
            if (srcCodeConfigs == null)
            {
                srcCodeConfigs = LoadConfig();
                AppInstance.SourceCodeConfig = srcCodeConfigs;
            }
            SrcCodeConfig prjconfig = null;
            
            foreach (SrcCodeConfig config in srcCodeConfigs)
            {
                if (config.ProjectId == projectId)
                {
                    prjconfig = config;
                    break;
                }
            }
            if(prjconfig == null)
            {
                prjconfig = new SrcCodeConfig();
                prjconfig.ProjectId = projectId;
                srcCodeConfigs.Add(prjconfig);
            }

            prjconfig.SetPath(terminalType, codeType, basePath);
           
            SaveConfig(srcCodeConfigs);
        }
    }
}
